package android.demo.intent;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.demo.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private static final String MY_ACTION = "android.demo.intent.MY_ACTION";
	private Button btn;
	private Button button;
	private Button btnSys;
	private Button btnHome;
	private EditText toEditText, subjectEditText, contentEditText;
	private Button btnSend;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.intent_main);
		btn = (Button) findViewById(R.id.btnIntent);
		button = (Button) findViewById(R.id.btnIntent01);
		btnSys = (Button) findViewById(R.id.btnIntent02);
		btnHome = (Button) findViewById(R.id.btnIntent03);
		
		toEditText = (EditText) findViewById(R.id.et01);
		subjectEditText = (EditText) findViewById(R.id.et02);
		contentEditText = (EditText) findViewById(R.id.et03);
		btnSend = (Button) findViewById(R.id.btnEmail);
		
		btn.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				ComponentName cn = new ComponentName(MainActivity.this, "com.beyondbit.android.demo.intent.MyActivity");
				Intent intent = new Intent();
				intent.setComponent(cn);
				startActivity(intent);
			}
		});
		
		button.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction(MY_ACTION);
				startActivity(intent);
			}
		});
		
		btnSys.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_GET_CONTENT);
				intent.setType("vnd.android.cursor.item/phone");
				startActivity(intent);
			}
		});
		
		btnHome.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				startActivity(intent);
			}
		});
		
		btnSend.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				String to = toEditText.getText().toString();
				String subject = subjectEditText.getText().toString();
				String content = contentEditText.getText().toString();
				Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
				emailIntent.setType("plain/text");
				emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
				emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
				emailIntent.putExtra(Intent.EXTRA_TEXT, content);

				startActivity(Intent.createChooser(emailIntent, "msg..."));
			}
		});
	}
}
